﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPages_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PreRender += new EventHandler(InitPage);
        }
    }

    protected void InitPage(object sender, EventArgs e)
    {
        InitData();
    }
    protected void InitData()
    {
        ////String sqlLuckDistrict = "SELECT district.id as district_id,district.name as district_name,order_dish_quantity "+
        ////                        " FROM district,orders,consumer,orders_category_dish"+
        ////    //" inner join  (SELECT sum(weight) as order_dish_quantity from orders_category_dish,orders "+
        ////                        //" WHERE orders.id=orders_id and orders.status=3) S "+
        ////                        " WHERE orders.status=3 and orders.consumer_id=consumer.id and consumer.district_id=district.id";
        //String sqlLuckDistrict = "SELECT district_id,name as district_name,order_dish_quantity from district inner join " +
        //                        " (SELECT district_id,sum(orders_category_dish.num) as order_dish_quantity " +
        //                        " FROM orders,consumer,address,orders_category_dish" +
        //                        " WHERE orders.status=3 and orders.consumer_id=consumer.id and consumer.address_id=address.id "+
        //                        " and orders.id=orders_category_dish.orders_id GROUP BY address.district_id) S" +
        //                        " ON S.district_id=district.id "+
        //                        " ORDER BY district.id asc";

        //DataSet ds_luckydistrict = DB.QuerySelect(sqlLuckDistrict);

        //luckydistrict_datagrid.DataSource = ds_luckydistrict;
        //luckydistrict_datagrid.DataBind();
    }
    protected void OnClickDetailBtn(object sender, DataGridCommandEventArgs e)
    {
        //// configuration
        //int once_weight_top = int.Parse(ConfigurationManager.AppSettings["weight_delivery"]);


        //// Get an available courier
        //String sqlGetCourier = "SELECT top 1 id,code,name,phone FROM courier WHERE available=1 and status>0 ORDER BY status desc";
        //DataSet ds_courier = DB.QuerySelect(sqlGetCourier);
        //if (ds_courier.Tables[0].Rows.Count != 1)
        //{
        //    notice_label.Text = "没有空闲的快递员";
        //    notice_label.ForeColor = System.Drawing.Color.Red;
        //    return;
        //}

        //String courier_id = ds_courier.Tables[0].Rows[0].ItemArray[0].ToString();
        //String code_courier = ds_courier.Tables[0].Rows[0].ItemArray[1].ToString();
        //String name_courier = ds_courier.Tables[0].Rows[0].ItemArray[2].ToString();
        //String mobile_courier = ds_courier.Tables[0].Rows[0].ItemArray[3].ToString();
        //String time = DateTime.Now.ToShortDateString();

        //// Add a delivery record for this delivery
        //String sqlInsertDelivery = "INSERT INTO delivery(courier_id) VALUES('"+courier_id+"') SELECT @@identity as delivery_id";
        //DataSet ds_deliveryid = DB.QuerySelect(sqlInsertDelivery);
        //String delivery_id = ds_deliveryid.Tables[0].Rows[0].ItemArray[0].ToString();

        //String district_id = luckydistrict_datagrid.DataKeys[e.Item.ItemIndex].ToString();
        //String district_name = luckydistrict_datagrid.Items[e.Item.ItemIndex].Cells[1].Text;
        //Button dbtn = (Button)luckydistrict_datagrid.Items[e.Item.ItemIndex].FindControl("detail_button");
        //dbtn.Text = "下一批";

        //String sqlOrderAndWeight = "SELECT orders.id as orders_id,weight,phone,building,door "+
        //                          " FROM orders inner join"+ 
        //                                "(	SELECT orders.id as orders_id,sum(num) as weight  "+
        //                                  " FROM orders,orders_category_dish  	"+
        //                                  " WHERE 		orders.status=3 and 		orders.id=orders_category_dish.orders_id "+
        //                                  " GROUP BY orders.id) S on S.orders_id=orders.id,"+
        //                                " consumer,address,position "+
        //                          " WHERE orders.consumer_id=consumer.id and consumer.address_id=address.id and"+
        //                          " address.position_id=position.id and address.district_id="+district_id+
        //                          " ORDER BY building asc,door asc,weight desc";

        //DataSet ds_orderandweight = DB.QuerySelect(sqlOrderAndWeight);
 
        //int cur_weight = 0;
        //int counter = 0;
        //for (counter = 0; counter < ds_orderandweight.Tables[0].Rows.Count; counter++)
        //{
        //    int weight = int.Parse(ds_orderandweight.Tables[0].Rows[counter].ItemArray[1].ToString());

        //    if (cur_weight + weight < once_weight_top)
        //    {
        //        cur_weight += weight;
        //    }
        //    else
        //    {
        //        if (cur_weight == 0)
        //        {
        //            counter++;
        //        }
        //        break;
        //    }
        //}

        //// No more to dispatch to courier.
        //if (counter == ds_orderandweight.Tables[0].Rows.Count) { dbtn.Enabled = false; }


        //String info;
        //info  = "【" + code_courier + "】" + name_courier+" " + time + "\n";
        //info += "《" + district_name + "》" + "\n";
        //info += "==================\n";
        //String order_id_list = "";
        //for (int i = 0; i < counter; i++)
        //{
        //    order_id_list += ds_orderandweight.Tables[0].Rows[i].ItemArray[0].ToString() + ",";
        //}
        //order_id_list += "-1";

        //// set this orders to this delivery and update the status
        //String sqlUpdateOrder = "UPDATE orders set status=status+1,delivery_id=" + delivery_id + " WHERE id in (" + order_id_list+")";
        //DB.QueryAlter(sqlUpdateOrder);   

        //// get the detail info of the delivery and display
        //String[] report = DB.GetDeliveryReport(delivery_id);
        //info += report[5];
        //detail_order_textbox.Text = info;
        //notice_label.Text = "发给快递员 【" + code_courier + "】" + name_courier;

        //// Make this courier busy
        //String sqlBusyCourier = "UPDATE courier set status=0 where id=" + courier_id;
        //DB.QueryAlter(sqlBusyCourier);
    }
}
